package com.base.web.auth.service.impl;


import com.base.web.auth.service.UserService;
import com.base.web.core.basic.service.BasicServiceImpl;
import com.base.web.dao.master.auth.UserMapper;
import com.base.web.dao.master.auth.UserRoleMapper;
import com.base.web.model.master.auth.User;
import com.base.web.model.master.auth.UserRole;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @author: RENTIE
 * Date: 2018-07-05 8:57
 */
@Service
public class UserServiceImpl extends BasicServiceImpl<User> implements  UserService {

    @Resource
    private UserRoleMapper userRoleMapper;

    @Resource
    private UserMapper userMapper;

    /**
     * 用户角色绑定
     *
     * @param userId
     * @param userRoles
     * @return
     */
    @Override
    @Transactional
    @CacheEvict(value = "menus",key="'userMenu_'+#p0")
    public Integer roleBinding(String userId, List<UserRole> userRoles) {

        //删除用户角色关系
        userRoleMapper.delUserRoleByUserId(userId);

        //添加新的用户角色关系
        userRoles.forEach(ur -> {
            userRoleMapper.insertSelective(ur);
        });
        return 1;
    }

    @Override
    public List<String> queryRolesByLoginName(String loginName) {
        return userMapper.queryRolesByLoginName(loginName);
    }

    @Override
    public List<String> queryPermissionsByLoginName(String loginName) {
        return userMapper.queryPermissionsByLoginName(loginName);
    }

}
